<?php


class Resource_model extends Base_model {

	/**
	 * 表名
	 * 
	 * @param string 
	 */
    public $_table_name = "admin_resource";


    /**
     * 权限名称
     * @var string 
     */
    public $source_name;


    /**
     * 后台管理文件名及路径
     * @var string 
     */
    public $file_path;


	/**
     * 函数名 => 权限名称 数组（转换成字符串）
     * @var string 
     */
    public $resources;


    /**
     * 插入数据前验证
     * 
     * @return boolean 
     */
    protected function prepare_insert() {

		if(empty($this->source_name) OR empty($this->file_path) OR empty($this->resources))
		{
            return false;
        }

        return true;
    }

    
    /**
     * 搜索
     * 
     * @return boolean true | false
     */
    public function search() {
        $sql = "SELECT ";

        if(!empty($this->_selected_fields))
		{
			if (is_array($this->_selected_fields))
			{
				$sql .= implode(", ", $this->_selected_fields);
			}
			else
			{
				$sql .= $this->_selected_fields;
			}
        }
		elseif (!empty($this->_is_search_counter))
		{
            $sql .= " COUNT(adrc.id) AS rows_total ";
        }
		else
		{
            $sql .= " adrc.* ";
        }

        $sql .= " FROM {$this->_table_name} AS adrc ";

        $statement = "";
        $where = array();

        if (!empty($this->id)) {
            $statement .= " AND adrc.id = ? ";
            $where[] = $this->id;
        }

		//LIKE搜索转义
        if (!empty($this->source_name)) {
            $statement .= " AND adrc.source_name LIKE '%" . $this->db->escape_like_str($this->source_name). "%' ";
        }

		//LIKE搜索转义
        if (!empty($this->file_path)) {
            $statement .= " AND adrc.file_path LIKE '%" . $this->db->escape_like_str($this->file_path). "%' ";
        }

		//处理条件
        if($statement) {
            $sql .= " WHERE " . substr($statement, 4);
        }

        if (!empty($this->_group_by)) {
            $sql .= " GROUP BY {$this->_group_by} ";
        }

        if (!empty($this->_is_search_counter)) {
            $sql .= " LIMIT 1";
        } else {
            if (!empty($this->_order_by)) {
                $sql .= " ORDER BY {$this->_order_by} ";
            }

			//是否分页
            if ($this->is_paging()) {

				$page_from = ($this->_page_index - 1) * $this->_page_count;

                $sql .= " LIMIT {$page_from}, {$this->_page_count}";
            }
        }

        $result = $this->db->query($sql, $where);

        if(empty($this->_is_search_counter))
		{
			$this->_search_results = array();

            if ($result->num_rows() > 0) {
                $this->_search_results = $result->result();
            }
        }
		else
		{
            if ($result->num_rows() > 0) {
                $row = $result->row();

                if (!empty($row->rows_total)) {
                    $this->_search_num_rows = $row->rows_total;
                }
            }
        }

        $result->free_result();

        return true;
    }


    /**
     * 根据后台文件路径retrieve
     * 
     * @param string $file_path  后台管理文件路径
     * @return boolean true | false
     */
    public function retrieve_by_file_path($file_path)
	{
        if(empty($file_path))
		{
            return false;
        }

        $sql = "SELECT * FROM {$this->_table_name} WHERE file_path = ? ORDER BY id DESC LIMIT 1";

        $result = $this->db->query($sql, array($file_path));

		$re = false; //初始化返回结果

        if($result->num_rows() > 0)
		{
            $this->result_load($result->row());

            if(!$this->is_empty())
			{
				$re = true;
            }
        }

		$result->free_result();

		return $re;
	}


}